package com.example.demo.utils;

import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
 * @Description 将本地文件（String）转为MultipartFile
 * @Author chenhuiyu
 * @Date 2023/12/8
 */
public class GetMultipartFile {
    public static MultipartFile createMultipartFile(String fileName) {
        MultipartFile multipartFile;
        try {
            multipartFile = new MockMultipartFile(fileName, fileName, "text/plain", Files.newInputStream(Paths.get(fileName)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return multipartFile;
    }
}
